//
//  BaseViewController.m
//  news_ios_app
//
//  Created by apple on 16/3/16.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "BaseViewController.h"
#import "DefineConstant.h"
#import "View_Tool.h"
#import "BaseTableViewCell.h"

@interface BaseViewController ()

@property (nonatomic, strong) NSMutableArray *data;

@end

@implementation BaseViewController

@synthesize backSwipeGestureRecognizer = _backSwipeGestureRecognizer;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.titleTextAttributes = @{UITextAttributeTextColor : [UIColor whiteColor]};
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    _backSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(popViewController)];
    _backSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    _backSwipeGestureRecognizer.cancelsTouchesInView = NO;
    _backSwipeGestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:_backSwipeGestureRecognizer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)popViewController
{
    if (self.navigationController != nil) {
        if ([self.navigationController.viewControllers count] > 1){
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (void)showWaitAlert
{
    [self showWaitAlert:nil];
}

- (void)showWaitAlert:(NSString*)alertString
{
    NSString *message = alertString;
    if (IsNilOrNull(alertString) || [alertString isEqualToString:@""]) {
        message = @"正在加载中...";
    }
    [View_Tool alert:message type:AlertTypeWait autoHide:NO inView:self.view];
}

- (void)hideWaitAlert
{
    [View_Tool hideAlertInView:self.view];
}

- (void)enterViewController:(UIViewController *)vc
{
    UIViewController *nextVC = nil;
    for (UIViewController *item in self.navigationController.viewControllers) {
        if ([item isKindOfClass:[vc class]]) {
            nextVC = item;
        }
    }
    
    if (nextVC) {
        [self.navigationController popToViewController:nextVC animated:YES];
    } else {
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - button actions
-(void)backAction:(id)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)searchAction:(id)sender{
    
}

-(void)rightAction:(id)sender
{
    
} //由子类实现


#pragma mark - setter && getter

-(UIButton *)backBtn{
    
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _backBtn.frame = CGRectMake(0, 0, 44, 44);
        _backBtn.backgroundColor = [UIColor clearColor];
        [_backBtn setExclusiveTouch:YES];
        [_backBtn setImage:[UIImage imageNamed:@"shared_back_icon"] forState:UIControlStateNormal];
        
        [_backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

-(UIButton *)searchBtn{
    
    if (!_searchBtn) {
        
        _searchBtn = [[UIButton alloc] init];
        _searchBtn.frame = CGRectMake(0, 0, 44, 44);
        _searchBtn.backgroundColor = [UIColor clearColor];
        [_searchBtn setExclusiveTouch:YES];
        [_searchBtn addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _searchBtn;
}

-(UIButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [[UIButton alloc] init];
        _rightBtn.frame = CGRectMake(0, 0, 44, 44);
        _rightBtn.backgroundColor = [UIColor clearColor];
        [_rightBtn setExclusiveTouch:YES];
        [_rightBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
        [_rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rightBtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _rightBtn;
}

- (BaseTableView *)baseTableView
{
    if (!_baseTableView) {
        _baseTableView = [[BaseTableView alloc]init];
        _baseTableView.backgroundColor = [UIColor clearColor];
        _baseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _baseTableView.delegate = self;
        _baseTableView.dataSource = self;
        _baseTableView.exclusiveTouch = YES;
    }
    return _baseTableView;
}


#pragma mark - dataArr
- (void)setDataArr:(NSMutableArray *)dataArr
{
    if (dataArr) {
        _data = dataArr;
    }else{
        _data = nil;
    }
}

#pragma mark - tableview dataSource && delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellType = [NSString stringWithFormat:@"%@",[[_data objectForIndex:indexPath.row] class]];
    BaseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellType forIndexPath:indexPath];
    [cell updateCellWithModel:[_data objectForIndex:indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  [[_data objectAtIndex:indexPath.row ]heightWithModel];;
}


#pragma mark - UIScrollViewDelegate - 预加载

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:self.baseTableView]) {
        //加载更多的条件计算为滚动到倒数第2页的时候处理是否可以加载更多
        if(scrollView.contentOffset.y > (scrollView.contentSize.height - BOUNDHEIGHT*2)){
#pragma mark - 还是要判断是否还有下一页数据
//            if ([self.viewModel hasMoreItem] && ![self.baseTableView.mj_footer isRefreshing]) {
//                [self.baseTableView.mj_footer beginRefreshing];
//            }
            if (![self.baseTableView.mj_footer isRefreshing]) {
                [self.baseTableView.mj_footer beginRefreshing];
            }
        }
    }
}

@end
